package com.aiit.controller;

import com.aiit.pojo.LingJian;
import com.aiit.pojo.Merchant;
import com.aiit.pojo.User;
import com.aiit.pojo.order.PurchaseOrder;
import com.aiit.service.ILingJianService;
import com.aiit.service.IMerchantService;
import com.aiit.service.IPurchaseOrderService;
import com.aiit.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.*;

@Controller
@RequestMapping("/main/warehouse")
public class WarehouseController {
    @Autowired
    IPurchaseOrderService purchaseOrderService;
    @Autowired
    IUserService userService;
    @Autowired
    ILingJianService lingJianService;
    @Autowired
    IMerchantService merchantService;

    @RequestMapping("/damage")
    public String toDamage(){
        return "/main/warehouse/damage";
    }
    @RequestMapping("/alarm")
    public String toAlarm(){
        return "/main/warehouse/alarm";
    }
    @RequestMapping("/brim")
    public String toBrim(){
        return "/main/warehouse/brim";
    }
    @RequestMapping("/brimordamage")
    public String toBrimOrDamage(){
        return "/main/warehouse/brimordamage";
    }
    @RequestMapping("/querywarehouse")
    public String toQueryWarehouse(){
        return "/main/warehouse/querywarehouse";
    }
    //加载报损报溢查询条件
    @ResponseBody
    @RequestMapping("/login/damage")
    public Map<String,Object> loginDamage(){
        Map<String, Object> map = new HashMap<>();
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll4();
        List<User> users = userService.queryAll2();
        map.put("purchaseOrders",purchaseOrders);
        map.put("users",users);
        System.out.println(map);
        return map;
    }
    //更新订单的状态
    @ResponseBody
    @RequestMapping("/update/damage")
    public Integer updateDamage(@RequestParam("id")Integer id,@RequestParam("userId")Integer userId,@RequestParam("trueNumber")Integer trueNumber,@RequestParam("bdTime")String bdTime) throws ParseException {
        System.out.println(id+userId+trueNumber+bdTime);
        if (purchaseOrderService.queryById4(id).getUserId() != userId){
            return 2;
        }
        PurchaseOrder purchaseOrder = purchaseOrderService.queryByUId(id,userId);
        System.out.println(purchaseOrder);
        purchaseOrder.setTrueNumber(trueNumber);
        if (purchaseOrder.getNumber()>purchaseOrder.getTrueNumber()){
            purchaseOrder.setBrimDamage(1);
        }else if (purchaseOrder.getNumber()<purchaseOrder.getTrueNumber()){
            purchaseOrder.setBrimDamage(2);
        }else {
            purchaseOrder.setBrimDamage(0);
        }
        purchaseOrder.setBdTime(bdTime);
        purchaseOrder.setUpdateTime(new Date());
        purchaseOrder.setTrueTotalAmount(((double)trueNumber)*lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getPrice());
        purchaseOrder.setState(2);
        Merchant merchant = merchantService.getOne(new QueryWrapper<Merchant>().eq("id", purchaseOrder.getMerchantId()));
        merchant.setSuccessNumber(merchant.getSuccessNumber()+1);
        if(purchaseOrderService.updateById3(purchaseOrder)){
            LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId()));
            lingJian.setNumber(lingJian.getNumber()+purchaseOrder.getTrueNumber());
            lingJian.setTotalNumber(lingJian.getNumber()+purchaseOrder.getTrueNumber());
            if (lingJian.getNumber()+purchaseOrder.getTrueNumber()>500){
                lingJian.setState("正常");
            }
            if (lingJianService.updateById(lingJian)){
                return 1;
            }else {
                return 0;
            }
        }else {
            return 0;
        }
    }
    //加载库存报警查询条件
    @ResponseBody
    @RequestMapping("/login/alarm")
    public Map<String,Object> loginAlarm(){
        Map<String, Object> map = new HashMap<>();
        List<LingJian> lingJians = lingJianService.queryAll2();
        System.out.println(lingJians);
        map.put("lingJians",lingJians);
        return map;
    }
    //根据条件查找相关的报警库存
    @ResponseBody
    @RequestMapping("/select/alarm")
    public Map<String,Object> selectAlarm(@RequestParam("id")String id){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索"))){
            LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", Integer.parseInt(id)));
            map.put("lingjian",lingjian);
            map.put("result",1);
            return map;
        }else {
            List<LingJian> lingJians = lingJianService.queryAll2();
            map.put("lingjians",lingJians);
            map.put("result",0);
            return map;
        }
    }
    //加载报损报溢的订单的
    @ResponseBody
    @RequestMapping("/login/brimordamage")
    public Map<String,Object> loginBrimOrDamage(){
        Map<String, Object> map = new HashMap<>();
        List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll5();
        List<LingJian> lingJians = lingJianService.queryAll();
        List<User> users = userService.queryAll2();
        map.put("purchaseOrders",purchaseOrders);
        map.put("lingJians",lingJians);
        map.put("users",users);
//        System.out.println("_______________________________________________"+purchaseOrders);
        return  map;
    }
    //根据查找条件查找相应订单
    @ResponseBody
    @RequestMapping("/select/brimordamage")
    public Map<String,Object> selectBrimOrDamage(@RequestParam("id")String id,@RequestParam("lingjianName")String lingjianName,@RequestParam("userId")String userId){
        Map<String, Object> map = new HashMap<>();
        if (!(id.equals("直接选择或搜索"))){
            PurchaseOrder purchaseOrder = purchaseOrderService.queryById5(Integer.parseInt(id));
            purchaseOrder.setLingjianName(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName());
            purchaseOrder.setUserName(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName());
            System.out.println(purchaseOrder);
            map.put("purchaseOrder",purchaseOrder);
            map.put("result",1);
        }else if (!(lingjianName.equals("直接选择或搜索"))&&!(userId.equals("直接选择或搜索"))){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByLUId(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianName)).getId(),Integer.parseInt(userId));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setLingjianName(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setUserName(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName()));
            System.out.println(purchaseOrders);
            map.put("purchaseOrders",purchaseOrders);
        }else if (!(lingjianName.equals("直接选择或搜索"))){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByLId3(lingJianService.getOne(new QueryWrapper<LingJian>().eq("name",lingjianName)).getId());
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setLingjianName(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setUserName(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName()));
            System.out.println(purchaseOrders);
            map.put("purchaseOrders",purchaseOrders);
        }else  if (!(userId.equals("直接选择或搜索"))){
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryByUId2(Integer.parseInt(userId));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setLingjianName(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setUserName(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName()));
            System.out.println(purchaseOrders);
            map.put("purchaseOrders",purchaseOrders);
        }else {
            List<PurchaseOrder> purchaseOrders = purchaseOrderService.queryAll6();
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setLingjianName(lingJianService.getOne(new QueryWrapper<LingJian>().eq("id",purchaseOrder.getLingjianId())).getName()));
            purchaseOrders.forEach(purchaseOrder -> purchaseOrder.setUserName(userService.getOne(new QueryWrapper<User>().eq("id",purchaseOrder.getUserId())).getName()));
            System.out.println(purchaseOrders);
            map.put("purchaseOrders",purchaseOrders);
        }
        return map;
    }
    //删除报损报溢的订单
    @ResponseBody
    @RequestMapping("/delete/brimordamage")
    public Integer deleteBrimOrDamage(@RequestParam("id")Integer id){
        Integer result = purchaseOrderService.deleteById(id);
        return result;
    }
    //查找栏信息加载
    @ResponseBody
    @RequestMapping("/login/querywarehouse")
    public Map<String,Object> loginQueryWareHouse(){
        Map<String, Object> map = new HashMap<>();
        List<LingJian> lingJians = lingJianService.queryAll();
        map.put("lingJians",lingJians);
        return map;
    }
    //根据查找信息查找库存
    @ResponseBody
    @RequestMapping("/select/querywarehouse")
    public Map<String,Object> selectQueryWareHouse(@RequestParam("id") String id,@RequestParam("name")String name){
        Map<String, Object> map = new HashMap<>();
        System.out.println(id+name);
        if (!(id.equals("直接选择或搜索"))){
            LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", Integer.parseInt(id)));
            map.put("lingJian",lingJian);
        }else if (!(id.equals("直接选择或搜索"))){
            LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("name", name));
            map.put("lingJian",lingJian);
        }else {
            List<LingJian> lingJians = lingJianService.queryAll();
            map.put("lingJians",lingJians);
            map.put("result",2);
        }
        return map;
    }
    //获取商品信息传递到修改信息页面
    @ResponseBody
    @RequestMapping("/update/querywarehouse")
    public Map<String,Object> updateQueryWareHouse(@RequestParam("id")Integer id){
        Map<String, Object> map = new HashMap<>();
        LingJian lingjian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", id));
        map.put("lingjian",lingjian);
        return map;
    }
    @RequestMapping("tolingjianupdate")
    public String toLingJianUpdate(){
        return "/main/warehouse/lingjianupdate";
    }
    //修改商品的信息
    @ResponseBody
    @RequestMapping("/update/lingjianupdate")
    public Integer lingJianUpdate(@RequestParam("id")Integer id,@RequestParam("price")Double price,@RequestParam("number")Integer number,@RequestParam("totalNumber")Integer totalNumber){
        System.out.println("id"+id+"price"+price+"number"+number+"totalNumber"+totalNumber);
        LingJian lingJian = lingJianService.getOne(new QueryWrapper<LingJian>().eq("id", id));
        lingJian.setPrice(price);
        lingJian.setNumber(number);
        lingJian.setTotalNumber(totalNumber);
        if (lingJianService.updateById(lingJian)){
            return 1;
        }
        return 0;
    }

}
